/*
 * @Author: guojiangang guojiangang@hikvision.com.cn
 * @Date: 2024-02-21 20:21:22
 * @LastEditors: guojiangang guojiangang@hikvision.com.cn
 * @LastEditTime: 2024-02-22 10:04:07
 * @FilePath: \cfase:\leetcode\1.两数之和.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @lc app=leetcode.cn id=1 lang=javascript
 *
 * [1] 两数之和
 */

// @lc code=start
/**
 * 暴力枚举
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
// var twoSum = function(nums, target) {
//   var len = nums.length
//   var result = []
//   for (var i = 0; i < len - 1; i++) {
//     for (var j = i + 1; j < len; j++) {
//       if (nums[i] + nums[j] === target) {
//         result = [i, j]
//         break
//       }
//     }
//     if (result.length) {
//       break
//     }
//   }
//   return result
// };
/**
 * 哈希表
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
  const map = new Map()
  const len = nums.length
  for (let i = 0; i < len; i++) {
    const temp = target - nums[i]
    if (map.has(temp)) {
      return [map.get(temp), i]
    }
    // const temp = map.get(target - nums[i])
    // if (temp !== undefined) {
    //   return [temp, i]
    // }
    map.set(nums[i], i)
  }
};
// @lc code=end

